<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

class NavController extends AdminBaseController
{
    const CNAME = '导航菜单';

    protected $nav_model;
    protected $navCate_model;

    public function _initialize() {
        parent::_initialize();
        $this->nav_model     = D("Common/Nav");
        $this->navCate_model = D("Common/NavCate");
        $this->assign('TITLE', self::CNAME);
    }

    /**
     * 前台菜单列表
     */
    public function index() {
        //不存在默认为1,主导航
        $cate_id = I('post.cate_id', 1, 'intval');
        $navs = $this->nav_model->getNavsByCateId($cate_id);
        $navs = x_get_tree($navs, 'nav_id', 'parent_id', 'nav_name');
        $nav_cates = $this->navCate_model->fetchAll();
        $this->assign('navCates', $nav_cates);
        $this->assign('navs', $navs);
        $this->assign('cate_id', $cate_id);
        $this->display();
    }

    /**
     * 添加导航
     */
    public function add() {

        if (IS_POST) {
            $data = $this->nav_model->create();
            if ($data) {
                $id = $this->nav_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->nav_model->getError());
            }
        } else {
            $cate_id   = I('get.cate_id', 1, 'intval');
            $parent_id = I('get.parent_id', 0, 'intval');
            //导航分类
            $nav_cates = $this->navCate_model->fetchAll();
            //导航
            $navs = $this->nav_model->getNavsByCateId($cate_id);
            $navs = x_get_tree($navs, 'nav_id', 'parent_id', 'nav_name');

            //可选择的导航列表

            $navList = $this->_select();

            foreach ($navList as $key=>$navdata) {
                $navListTree = x_get_tree($navdata['items'], 'id', 'parentid', 'label');
                $navList[$key]['tree']=$navListTree;
                unset( $navList[$key]['items']);
            }
            $this->assign('navs', $navs);
            $this->assign('navCates', $nav_cates);
            $this->assign('navList', $navList);
            $this->assign('cate_id', $cate_id);
            $this->assign('parent_id', $parent_id);
            $this->display();
        }
    }

    /**
     * 编辑导航
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->nav_model->create();
            if ($data) {
                if ($this->nav_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->nav_model->getError());
            }
        } else {
            $nav_id = I('get.nav_id', 0, 'intval');
            $detail = $this->nav_model->find($nav_id);
            //导航分类
            $nav_cates = $this->navCate_model->fetchAll();
            //导航
            $navs = $this->nav_model->getNavsByCateId($detail['cate_id']);
            $navs = x_get_tree($navs, 'nav_id', 'parent_id', 'nav_name');

            $this->assign('detail', $detail);
            $this->assign('navs', $navs);
            $this->assign('navCates', $nav_cates);
            $this->display();
        }
    }

    /**
     * 删除前台菜单
     */
    public function delete() {
        $nav_id = I('post.nav_id', 0, 'intval');

        if (is_numeric($nav_id)) {
            $this->nav_model->delete($nav_id);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的'.self::CNAME);
        }
    }

    /**
     * 显示或不显示导航菜单
     */
    public function changeStatus() {

        $nav_id = I('post.nav_id', 0, 'intval');

        if (!empty($nav_id)) {

            if (!$detail = $this->nav_model->find($nav_id)) {
                $this->error('请选择要改变状态的' . self::CNAME);
            }
            $data['nav_id'] = $nav_id;
            $data['status'] = $detail['status'] ? 0 : 1 ;
            if ($this->nav_model->save($data) !== false) {
                $this->success(self::CNAME . '状态改变成功', U('index'));
            } else {
                $this->error(self::CNAME . '状态改变失败');
            }

        } else {
            $this->error('请选择要改变状态的' . self::CNAME);
        }
    }

    /**
     * 更新导航菜单排序
     */
    public function updateSort() {
        $status = parent::_updateListSort($this->nav_model);
        if ($status) {
            $this->success("更新排序成功", U('index'));
        } else {
            $this->error("更新排序失败");
        }
    }

    /**
     * select nav
     */
    private function _select() {
        $apps = x_scan_dir(APP_PATH . '*');
        $navList = array();
        foreach ($apps as $app){
            if (is_dir(APP_PATH . $app)) {
                if (!(strpos($app, ".") === 0)) {
                    $navfile = APP_PATH . $app . "/nav.php";
                    if(file_exists($navfile)){
                        $navgeturls = include $navfile;
                        foreach ($navgeturls as $url){
                            $nav = R("$app/$url");

                            $navList[]=$nav;
                        }
                    }
                }
            }
        }
        return $navList;
    }
}